package com.demo.utils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {

    public List<String> loadResourceList(String path) {

        String fileName = this.getClass().getClassLoader().getResource(path).getPath();
        try {
            String osName = System.getProperty("os.name");
            //System.out.println(osName);
            if(osName.startsWith("win") || osName.startsWith("Win")) {
                if(fileName.startsWith("/")) {
                    fileName = fileName.substring(1);
                }
            }
            //System.out.println(fileName);
            List<String> lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
            return lines;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    public void write2File(String path, String content, boolean append) {

        try {
            StandardOpenOption openOption = StandardOpenOption.TRUNCATE_EXISTING;
            if (append) {
                openOption = StandardOpenOption.APPEND;
            }
            Path p = Paths.get(path);
            Files.write(p, content.getBytes(StandardCharsets.UTF_8), openOption);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void write2File(String path, String content) {

        try {
            Path p = Paths.get(path);
            Files.deleteIfExists(p);
            Files.createFile(p);
            Files.write(p, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void appendFile(String path, String content) {

        try {
            Path p = Paths.get(path);
            Files.write(p, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String loadResourceString(String path) {

        String LINE_SEP = System.getProperty("line.separator");
        String fileName = this.getClass().getClassLoader().getResource(path).getPath();
        String osName = System.getProperty("os.name");
        //System.out.println(osName);
        if(osName.startsWith("win") || osName.startsWith("Win")) {
            if(fileName.startsWith("/")) {
                fileName = fileName.substring(1);
            }
        }
        //System.out.println(fileName);
        try {
            List<String> lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
            StringBuilder sb = new StringBuilder();
            for (String s : lines) {
                sb.append(s);
                sb.append(LINE_SEP);
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    private final String STR_FORMAT00 = "00";
    private final String STR_FORMAT000 = "000";

    //格式化补0
    protected String toStringWith00(int num) {
        DecimalFormat df = new DecimalFormat(STR_FORMAT00);
        return df.format(num);
    }

    protected String toStringWith000(int num) {
        DecimalFormat df = new DecimalFormat(STR_FORMAT000);
        return df.format(num);
    }
}
